package com.maxjay.system.web.controller.struts;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.maxjay.system.core.Application;
import com.maxjay.system.core.cache.BufferedDictTablePool;
import com.maxjay.system.core.cache.SystemDataHolder;

/**
 * 缓存管理Servlet
 * 
 * @author zhaixm
 *
 */
public class CacheManageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public CacheManageServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		if (StringUtils.isNotBlank(action)) {
			try {
				Method method = this.getClass().getMethod(action, HttpServletRequest.class, HttpServletResponse.class);
				method.setAccessible(true);
				method.invoke(this, request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		request.getRequestDispatcher("/cacheManage.jsp").forward(request, response);
	}
	
	/**
	 * 重新加载指定实体
	 * 
	 * @param request
	 * @param response
	 */
	public void reloadEntity(HttpServletRequest request, HttpServletResponse response) {
		String entityId = request.getParameter("entityId");
		Application.getEntityContext().getEntityFactory().loadEntity(entityId);
	}
	
	/**
	 * 重新加载指定查询方案
	 * 
	 * @param request
	 * @param response
	 */
	public void reloadQueryScheme(HttpServletRequest request, HttpServletResponse response) {
		String schemeId = request.getParameter("schemeId");
		Application.getEntityContext().getQuerySchemeFatory().loadQueryScheme(schemeId);
	}

	/**
	 * 重新加载所有实体配置
	 * 
	 * @param request
	 * @param response
	 */
	public void reloadAllEntity(HttpServletRequest request, HttpServletResponse response) {
		Application.getEntityContext().getEntityFactory().reloadAll();
	}
	
	/**
	 * 重新加载所有查询方案
	 * 
	 * @param request
	 * @param response
	 */
	public void reloadAllQueryScheme(HttpServletRequest request, HttpServletResponse response) {
		Application.getEntityContext().getQuerySchemeFatory().reloadAll();
	}
	
	/**
	 * 重新加载表码缓存
	 * 
	 * @param request
	 * @param response
	 */
	public void reloadAllDictTab(HttpServletRequest request, HttpServletResponse response) {
		BufferedDictTablePool.reset();
	}
	
	/**
	 * 重新加载菜单缓存
	 * 
	 * @param request
	 * @param response
	 */
	public void reloadMenuTree(HttpServletRequest request, HttpServletResponse response) {
		SystemDataHolder.refresh();
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
